<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class seleksi extends CI_Controller {
	
	var $objectid = 'seleksi';
	
	function __construct(){
		parent::__construct();
		$this->load->model('sd/psb/mdl_seleksi');
		
		$this->load->model('sd/setting/mdl_proses_penerimaan');
		$this->load->model('sd/setting/mdl_kelompok_penerimaan');
		//$this->output->enable_profiler(TRUE);
	}
	
	function index(){
		$data['objectid'] = $this->objectid;
		$this->load->view('sd/psb/seleksi', $data);
	}
	
	function grid(){
		$data = $this->mdl_seleksi->get_data();
		echo $this->mdl_seleksi->togrid($data['row_data'], $data['row_count']);
	}
	
	/*
	function grid_seleksi(){
		$data = $this->mdl_seleksi->get_data_seleksi();
		echo $this->mdl_seleksi->togrid($data['row_data'], $data['row_count']);
	}
	*/
	
	/*
	function add(){
		$data['objectid'] = $this->objectid;
		
		$this->load->view('sd/psb/seleksi_form', $data);
	}
	*/
	
	function edit($id){
		$data['objectid'] = $this->objectid;
		
		$r = $this->mdl_seleksi->get_data_edit($id);
		
		$data['id'] = $r->row()->id;
		$data['no_pendaftaran'] = $r->row()->no_pendaftaran;
		$data['nama_psb'] = $r->row()->nama_psb;
		$data['nama_kelompok'] = $r->row()->nama_kelompok;
		$data['nama'] = $r->row()->nama;
		$data['status'] = $r->row()->status;
		
		$this->load->view('sd/psb/seleksi_form_edit', $data);
	}
	
	function save($aksi){
		# init
		$status = "";
		$result = false;
		$data['pesan_error'] = '';
		
		# get post data
		foreach($_POST as $key => $value){
			$data[$key] = $value;
		}
		//$data['nama_kelompok'] = strtoupper($data['nama_kelompok']);
		
		# rules validasi form
		if($aksi=='add'){
			$this->form_validation->set_rules('id_psb', 'Proses Penerimaan', 'trim|required|xss_clean');
			$this->form_validation->set_rules('id_kelompok', 'Gelombang', 'trim|required|xss_clean');
		}else{
			$this->form_validation->set_rules('id', 'id calon siswa', 'trim|required|xss_clean');
		}
			
		# message rules
		$this->form_validation->set_message('required', 'Field %s harus diisi.');
		$this->form_validation->set_message('numeric', 'Field %s harus diisi dengan angka.');
		$this->form_validation->set_message('email', 'Field %s harus diisi dengan alamat email yang benar.');

		$data['pesan_error'] = '';
		if ($this->form_validation->run() == FALSE){
			$data["pesan_error"].= trim(form_error('id',' ',' '))==''?'':form_error('id',' ',' ').'<br>';
			$data["pesan_error"].= trim(form_error('id_psb',' ',' '))==''?'':form_error('id_psb',' ',' ').'<br>';
			$data["pesan_error"].= trim(form_error('id_kelompok',' ',' '))==''?'':form_error('id_kelompok',' ',' ').'<br>';
			
		}else{
			if($aksi=="add"){ // add
				//$result = $this->mdl_seleksi->save_seleksi($data);
			}else{ // edit
				$result=$this->mdl_seleksi->edit_status($data);
			}
		}
		
		if($result){
			echo json_encode(array('success'=>true));
		}else{
			echo json_encode(array('msg'=>$data['pesan_error']));
		}
	}
	
	function delete($kode){
		$result = $this->mdl_seleksi->delete($kode);
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'Data gagal dihapus'));
		}
	}
	
	function set_status(){
		$id = $this->input->post('id');
		$stat = $this->input->post('stat');
		
		echo $this->mdl_seleksi->set_status($id, $stat);
		//echo "ID : $id, Stat : $stat";
	}
	
}